//
//  AuthCodeViewController.m
//  HanlinWeather
//
//  Created by 任翰林 on 2017/4/9.
//  Copyright © 2017年 任翰林. All rights reserved.
//

#import "AuthCodeViewController.h"
#import <Masonry.h>
#import <SMS_SDK/SMSSDK.h>
@interface AuthCodeViewController ()
@property(nonatomic ,assign)NSInteger count;
@property(nonatomic, assign)UIButton * btn;
@property(nonatomic,strong)UITextField * numTextField;
@property(nonatomic,strong)UITextField * codeTextField;
@property(nonatomic,strong)UITextField * passTextField;
@end

@implementation AuthCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = KWhiteColor;
    [self setUpUI];
}


- (void)setUpUI
{
    
    UILabel * namelable = [UILabel new];
    [self.view addSubview:namelable];
    namelable.text = @"短信验证以及倒计时demo";
    namelable.font = [UIFont systemFontOfSize:25];
    [namelable mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(@120);
        make.centerX.equalTo(@0);
    }];
    
    
    
    
    //电话号码输入框
    UITextField * numTextField = [[UITextField alloc]init];
    [self.view addSubview:numTextField];
    self.numTextField = numTextField;
    numTextField.placeholder = @"请输入电话号码";
    numTextField.keyboardType = UIKeyboardTypeNumberPad;
    [numTextField addTarget:self action:@selector(numTextField:) forControlEvents:UIControlEventEditingChanged];
    [numTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(@20);
        make.top.equalTo(@180);
        make.height.equalTo(@40);
        make.right.equalTo(@-20);
    }];
    
    
    //电话号码输入框下面的线条
    UIView * line1 = [[UIView alloc]init];
    [self.view addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(@20);
        make.right.equalTo(@-20);
        make.top.equalTo(numTextField.mas_bottom).offset(4);
        make.height.equalTo(@1);
    }];
    line1.backgroundColor = KLightGray;
    
    
    //验证码输入框
    UITextField * codeTextField = [[UITextField alloc]init];
    [self.view addSubview:codeTextField];
    codeTextField.placeholder = @"请输入验证码";
    codeTextField.keyboardType = UIKeyboardTypeNumberPad;
    self.codeTextField = codeTextField;
    [codeTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(@20);
        make.right.equalTo(@-150);
        make.height.equalTo(@40);
        make.top.equalTo(line1.mas_bottom).offset(10);
    }];
    
    //验证码获取按钮
    UIButton * btn = [[UIButton alloc]init];
    self.btn = btn;
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(codeTextField.mas_right);
        make.right.equalTo(@-20);
        make.height.equalTo(codeTextField);
        make.top.equalTo(codeTextField);
        
    }];
    btn.backgroundColor = KLightGray;
    btn.enabled = NO;
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn setTitle:@"点击获取验证码" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    
    //验证码输入框下面的线条
    UIView * line2 = [[UIView alloc]init];
    [self.view addSubview:line2];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(@20);
        make.right.equalTo(@-20);
        make.top.equalTo(codeTextField.mas_bottom).offset(4);
        make.height.equalTo(@1);
    }];
    line2.backgroundColor = KLightGray;
    
    //密码输入框
    UITextField * passTextField = [UITextField new];
    [self.view addSubview:passTextField];
    passTextField.placeholder = @"请输入密码；测试密码：123";
    self.passTextField = passTextField;
    [passTextField setSecureTextEntry:YES];
    [passTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(@20);
        make.right.equalTo(@-20);
        make.top.equalTo(line2.mas_bottom).offset(4);
        make.height.equalTo(@40);
    }];

    
    //密码码输入框下面的线条
    UIView * line3 = [[UIView alloc]init];
    [self.view addSubview:line3];
    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(@20);
        make.right.equalTo(@-20);
        make.top.equalTo(passTextField.mas_bottom).offset(4);
        make.height.equalTo(@1);
    }];
    line3.backgroundColor = KLightGray;
    
    //登录按钮
    UIButton * logInBtn = [UIButton new];
    [self.view addSubview:logInBtn];
    [logInBtn addTarget:self action:@selector(logInBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [logInBtn setTitle:@"登录" forState:UIControlStateNormal];
    logInBtn.backgroundColor = KGreenColour;
//    logInBtn.enabled = NO;
    [logInBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(@20);
        make.right.equalTo(@-20);
        make.height.equalTo(@40);
        make.top.equalTo(line3.mas_bottom).offset(10);
    }];
    
}

//登录按钮的点击事件
- (void)logInBtnClick:(UIButton *)sender
{

    [self.numTextField endEditing:YES];
    [self.codeTextField endEditing:YES];
    [self.passTextField endEditing:YES];

    /**
     * @from               v1.1.1
     * @brief              提交验证码(Commit the verification code)
     *
     * @param code         验证码(Verification code)
     * @param phoneNumber  电话号码(The phone number)
     * @param zone         区域号，不要加"+"号(Area code)
     * @param result       请求结果回调(Results of the request)
     */
    [SMSSDK commitVerificationCode:self.codeTextField.text phoneNumber:self.numTextField.text zone:@"86" result:^(SMSSDKUserInfo *userInfo, NSError *error) {
        
        {
            if (!error)
            {
                HLLog(@"验证成功");
                
                if ([self.passTextField.text isEqualToString:@"123"]) {

                    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"标题：登陆成功" message:@"信息：登录成功，验证码和密码都正确" preferredStyle:UIAlertControllerStyleActionSheet];
                    
                    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        
                        HLLog(@"确定按钮的回调");
                    }]];
                    [self presentViewController:alertController animated:YES completion:nil];

                }else{

                    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"标题：登陆失败" message:@"信息：验证码正确，但是密码错误" preferredStyle:UIAlertControllerStyleActionSheet];
                    
                    [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        
                        HLLog(@"确定按钮的回调");
                    }]];
                    [self presentViewController:alertController animated:YES completion:nil];
                }

            }else{

                UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"登录失败" message:[NSString stringWithFormat:@"错误信息：%@",error] preferredStyle:UIAlertControllerStyleActionSheet];
                
                [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    HLLog(@"确定按钮的回调");
                }]];
                [self presentViewController:alertController animated:YES completion:nil];
            }
        }
    }];
    
    
    
    
    
}

//手机号码输入的监听
- (void)numTextField:(UITextField *)sender
{
    
    if (sender.text.length == 11) {
        self.btn.enabled = YES;
        self.btn.backgroundColor = KGreenColour;
    }else{
        self.btn.enabled = NO;
        self.btn.backgroundColor = KLightGray;
    }
}






- (void)btnClick:(UIButton*)sender
{
    sender.enabled = NO;
    sender.backgroundColor = KLightGray;
    NSInteger  count = 15;
    self.count = count;
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeOperation:) userInfo:nil repeats:YES];
    self.btn.titleLabel.text = [NSString stringWithFormat:@"%ld",count];
    [self.btn setTitle:[NSString stringWithFormat:@"%ld",count] forState:UIControlStateNormal];
 
    
    //手机验证码获取要正码的方法；
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS
                            phoneNumber:self.numTextField.text
                                   zone:@"86"
                       customIdentifier:nil
                                 result:^(NSError *error) {
                                     HLLog(@"---------%@",self.numTextField.text);
                                     
                                     if (!error) {
                                         HLLog(@"成功");
                                     }else{
                                         HLLog(@"失败，原因：%@",error);
                                         UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"标题：获取验证码失败" message:[NSString stringWithFormat:@"错误信息：%@",error] preferredStyle:UIAlertControllerStyleActionSheet];
                                         
                                         [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                             
                                             HLLog(@"确定按钮的回调");
                                         }]];
                                         
//                                         [alertController addAction:[UIAlertAction actionWithTitle:@"警告" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//                                             
//                                             HLLog(@"警告按钮的回调");
//                                         }]];
//
//                                         [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
//                                             
//                                             HLLog(@"取消按钮的回调");
//                                         }]];

//                                         [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
//                                             
//                                             HLLog(@"标题的提示信息");
//                                         }];
                                         
                                         [self presentViewController:alertController animated:YES completion:nil];
                                     }
                                     
                                 }];

    
}
-(void)timeOperation:(NSTimer *)sender
{
    self.count--;
    [self.btn setTitle:[NSString stringWithFormat:@"%ld",self.count] forState:UIControlStateNormal];
    //    self.btn.enabled = YES;
    if (self.count == 0) {
        NSLog(@"%ld",self.count);
        self.btn.backgroundColor = KGreenColour;
        [sender invalidate];
        [self.btn setTitle:@"点击重新获取" forState:UIControlStateNormal];
        self.btn.enabled = YES;
    }
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self resignFirstResponder];
}


@end
